package cn.cmcc.online.smsapi.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.List;

import cn.cmcc.online.smsapi.ui.R;
import cn.cmcc.online.smsapi.ui.model.FlightData;
import cn.cmcc.online.smsapi.ui.model.MetaData;
import cn.cmcc.online.smsapi.ui.model.TableData;
import cn.cmcc.online.smsapi.ui.model.TransportSelectInfo;
import cn.cmcc.online.smsapi.ui.presenter.FlightPresenter;
import cn.cmcc.online.smsapi.ui.util.ContentUtil;
import cn.cmcc.online.smsapi.ui.widget.TransportSelectDialog;

public class FlightVistaImpl extends GeneralBaseVistaImpl implements FlightVista {
    private static final String TAG = "TrainVistaImpl";
    private Activity mActivity;
    private TextView mTextViewFlightNumber;
    private TextView mTextViewDepartCity;
    private TextView mTextViewArriveCity;
    private TextView mTextViewDepartAirport;
    private TextView mTextViewArriveAirport;
    private TextView mTextViewDepartTime;
    private TextView mTextViewArriveTime;
    private TextView mTextViewDepartDate;
    private TextView mTextViewArriveDate;
    private TextView mTextViewTableTitle1;
    private TextView mTextViewTableTitle2;
    private TextView mTextViewTableTitle3;
    private TextView mTextViewTableContent1;
    private TextView mTextViewTableContent2;
    private TextView mTextViewTableContent3;

    private TextView mTextViewTip;
    private View mImageViewDottedSplitLine;

    private FlightPresenter mFlightPresenter = null;
    private TransportSelectDialog mDialog;

    public FlightVistaImpl(Context context, FlightPresenter presenter) {
        super(context);

        mFlightPresenter = presenter;
        mFlightPresenter.setVista(this);

//        initView();
    }

    // MUST put after parent class constructor
//    private void initView() {
//        mTextViewFlightNumber.setOnClickListener(mSelectFlightClickListener);
//    }

    @Override
    protected void initLayout() {
        super.initLayout();
        super.addBody(View.inflate(mContext, R.layout.bubble_air_body, null));

        mTextViewFlightNumber = findView(R.id.bubble_air_number);

        mTextViewDepartCity = findView(R.id.bubble_depart_city);
        mTextViewArriveCity = findView(R.id.bubble_arrive_city);

        mTextViewDepartTime = findView(R.id.bubble_depart_time);
        mTextViewArriveTime = findView(R.id.bubble_arrive_time);
        mTextViewDepartDate = findView(R.id.bubble_depart_date);
        mTextViewArriveDate = findView(R.id.bubble_arrive_date);

        mTextViewDepartAirport = findView(R.id.bubble_depart_airport);
        mTextViewArriveAirport = findView(R.id.bubble_arrive_airport);

        mTextViewTableContent1 = findView(R.id.air_table_content_1);
        mTextViewTableContent2 = findView(R.id.air_table_content_2);
        mTextViewTableContent3 = findView(R.id.air_table_content_3);
        mTextViewTableTitle1 = findView(R.id.air_table_title_1);
        mTextViewTableTitle2 = findView(R.id.air_table_title_2);
        mTextViewTableTitle3 = findView(R.id.air_table_title_3);

        mImageViewDottedSplitLine = findView(R.id.bubble_air_split_line_img);
        mTextViewTip = findView(R.id.warm_air_info);
    }

    private View.OnClickListener mSelectFlightClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mFlightPresenter.queryFlightNumber();
        }
    };

    @Override
    public void bind(Context context, JSONObject data) {
        mActivity = (Activity) context;
        mFlightPresenter.bind(data);
        mFlightPresenter.completeFlightInfo();
        super.bind(context, data);
    }

    @Override
    public void enableFlightSelect(MetaData metaData) {
        if (mTextViewFlightNumber != null && metaData != null) {
            Drawable selectDrawable = mContext.getResources().getDrawable(R.drawable.bubble_select_train_number_icon);
            selectDrawable = ContentUtil.setDrawableColorFilter(selectDrawable, metaData.getHighlightColor());
            mTextViewFlightNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, selectDrawable, null);
            mTextViewFlightNumber.setCompoundDrawablePadding(mContext.getResources().getDimensionPixelOffset(R.dimen.bubble_air_body_area_air_select_air_icon_margin_left));
            mTextViewFlightNumber.setTextColor(Color.parseColor(metaData.getHighlightColor()));
            mTextViewFlightNumber.setOnClickListener(mSelectFlightClickListener);
        }
    }

    @Override
    public void disableFlightSelect() {
        if (mTextViewFlightNumber != null) {
            mTextViewFlightNumber.setTextColor(mContext.getResources().getColor(R.color.bubble_air_flight_num_color_disable));
            mTextViewFlightNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            mTextViewFlightNumber.setOnClickListener(null);
        }
    }

    @Override
    public void showFlightSelect(TransportSelectInfo selectInfo, int selected) {
        if (mDialog == null) {
            mDialog = new TransportSelectDialog(mActivity, selectInfo, selected);
            mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    mFlightPresenter.selectFlight(mDialog.getSelected());
                }
            });
        }
        if (!mActivity.isFinishing()) {
            mDialog.show();
        }
    }

    @Override
    public void showBody(FlightData data, MetaData metaData) {
        if (data == null) {
            return;
        }

        if (!TextUtils.isEmpty(data.getFlightNumber())) {
            mTextViewFlightNumber.setText(data.getFlightNumber());
        }

//        if (metaData != null && !TextUtils.isEmpty(metaData.getHighlightColor())) {
//            Drawable selectDrawable = mContext.getResources().getDrawable(R.drawable.bubble_select_train_number_icon);
//            selectDrawable = ContentUtil.setDrawableColorFilter(selectDrawable, metaData.getHighlightColor());
//            mTextViewFlightNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, selectDrawable, null);
//            mTextViewFlightNumber.setCompoundDrawablePadding(mContext.getResources().getDimensionPixelOffset(R.dimen.bubble_air_body_area_air_select_air_icon_margin_left));
//            mTextViewFlightNumber.setTextColor(Color.parseColor(metaData.getHighlightColor()));
//        }

        if (!TextUtils.isEmpty(data.getDepartAddress())) {
            mTextViewDepartCity.setText(data.getDepartAddress());
        } else {
            mTextViewDepartCity.setText(ContentUtil.NO_DATA);
        }

        if (!TextUtils.isEmpty(data.getArriveAddress())) {
            mTextViewArriveCity.setText(data.getArriveAddress());
        } else {
            mTextViewArriveCity.setText(ContentUtil.NO_DATA);
        }

        if (!TextUtils.isEmpty(data.getDepartTerminal())) {
            mTextViewDepartAirport.setText(data.getDepartTerminal());
        } else {
            mTextViewDepartAirport.setText(ContentUtil.NO_DATA);
        }

        if (!TextUtils.isEmpty(data.getArriveTerminal())) {
            mTextViewArriveAirport.setText(data.getArriveTerminal());
        } else {
            mTextViewArriveAirport.setText(ContentUtil.NO_DATA);
        }

        if (!TextUtils.isEmpty(data.getDepartTime())) {
            mTextViewDepartTime.setText(data.getDepartTime());
        } else {
            mTextViewDepartTime.setText(ContentUtil.NO_TIME);
        }

        if (!TextUtils.isEmpty(data.getArriveTime())) {
            mTextViewArriveTime.setText(data.getArriveTime());
        } else {
            mTextViewArriveTime.setText(ContentUtil.NO_TIME);
        }

        if (!TextUtils.isEmpty(data.getDepartDate())) {
            mTextViewDepartDate.setText(data.getDepartDate());
        } else {
            mTextViewDepartDate.setText(ContentUtil.NO_DATE);
        }

        if (!TextUtils.isEmpty(data.getArriveDate())) {
            mTextViewArriveDate.setText(data.getArriveDate());
        } else {
            mTextViewArriveDate.setText(ContentUtil.NO_DATE);
        }
    }

    @Override
    public void initDefaultView() {
        super.initDefaultView();
        // TODO
//        disableFlightSelect();

        mTextViewDepartTime.setText(ContentUtil.NO_TIME);
        mTextViewArriveTime.setText(ContentUtil.NO_TIME);
        mTextViewDepartDate.setText(ContentUtil.NO_DATE);
        mTextViewArriveDate.setText(ContentUtil.NO_DATE);
        mTextViewDepartCity.setText(ContentUtil.NO_DATA);
        mTextViewArriveCity.setText(ContentUtil.NO_DATA);
        mTextViewDepartAirport.setText(ContentUtil.NO_DATA);
        mTextViewArriveAirport.setText(ContentUtil.NO_DATA);

        mTextViewTip.setText(null);
        mImageViewDottedSplitLine.setVisibility(View.GONE);
        mTextViewTip.setVisibility(View.GONE);
    }

    @Override
    public void showTable(List<TableData> dataList) {
        if (dataList != null) {
            if (dataList.size() >= 1) {
                TableData data1 = dataList.get(0);
                mTextViewTableTitle1.setText(data1.getKey());
                mTextViewTableContent1.setText(data1.getValue());
                mTextViewTableTitle1.setVisibility(View.VISIBLE);
                mTextViewTableContent1.setVisibility(View.VISIBLE);
            }
            if (dataList.size() >= 2) {
                TableData data2 = dataList.get(1);
                mTextViewTableTitle2.setText(data2.getKey());
                mTextViewTableContent2.setText(data2.getValue());
                mTextViewTableTitle2.setVisibility(View.VISIBLE);
                mTextViewTableContent2.setVisibility(View.VISIBLE);
            }
            if (dataList.size() >= 3) {
                TableData data3 = dataList.get(2);
                mTextViewTableTitle3.setText(data3.getKey());
                mTextViewTableContent3.setText(data3.getValue());
                mTextViewTableTitle3.setVisibility(View.VISIBLE);
                mTextViewTableContent3.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void showNote(String note) {
        mTextViewTip.setText(note);
        mImageViewDottedSplitLine.setVisibility(View.VISIBLE);
        mTextViewTip.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismissDialog() {
        if (mActivity != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }
}
